﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Diagnostics;
using System.IO;

namespace NRainel.Log
{
    /// <summary>
    /// 文本日志
    /// </summary>
    public class TextLogRecord:IRecordLog
    {
        private string _logFileName;
        private string _errorLogFileName;
        public TextLogRecord(string errorLogFileName,string logFileName )
            : this(errorLogFileName)
        {
            _logFileName = logFileName;

            CheckPath(logFileName);
        }

        public TextLogRecord(string errorLogFileName)
        {
            _errorLogFileName = errorLogFileName;

            CheckPath(errorLogFileName);
        }

        private void CheckPath(string fileName)
        {
            string dir = Path.GetDirectoryName(fileName);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
        }

        #region IRecordLog 成员

        public void RecordLog(string log, LogTypeEnum logType)
        {
            switch (logType)
            {
                case LogTypeEnum.Error:
                    using (StreamWriter sw = new StreamWriter(_errorLogFileName,true))
                    {
                        sw.Write(log);
                    }
                    break;
                case LogTypeEnum.Info:
                    if (_logFileName != null)
                    {
                        using (StreamWriter sw = new StreamWriter(_logFileName,true))
                        {
                            sw.Write(log);
                        }
                    }
                    break;
            }
            
        }

        #endregion
    }
}
